package com.example.finaltool.component;

import com.example.finaltool.service.DeployService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author dengbw
 */
@Slf4j
@Component
public class LocalDeploy implements CommandLineRunner {
    ShutdownContext shutdownContext;

    DeployService service;

    @Autowired
    public LocalDeploy(ShutdownContext shutdownContext, DeployService service) {
        this.shutdownContext = shutdownContext;
        this.service = service;
    }

    @Value("${file}")
    private String filePath;

    @Value("${keep-running}")
    private String keepRunning;

    @Value("${go-depend}")
    private String goDepend;

    @Override
    public void run(String... args) throws Exception {
        if (StringUtils.isBlank(filePath)) {
            log.info("退出本地模式，启用服务模式");
        } else {
            log.info("开启本地模式，读取文件：\"" + filePath + "\"");
            service.deploy(filePath, "true".equals(goDepend));
            if (!"true".equals(keepRunning)) {
                shutdownContext.showdown();
            }
        }
    }
}
